import 'package:library_base/entities/distribution_child_entity.dart';
import 'package:library_base/entities/distribution_entity.dart';
import 'package:library_base/entities/event/dispatcher_goods_count.dart';
import 'package:library_base/entities/order_detail_entity.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:get/get.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

class ProgressController extends BaseListController<DistributionEntity, DistributionEntity> {
  /// 实体类
  RxList<DistributionEntity> listDataSelected = <DistributionEntity>[].obs;

  List<DistributionEntity> initlistDataSelected = <DistributionEntity>[];

  String stationTimeBegin = '';

  Rx<String> stationTimeEnd = ''.obs;

  @override
  void onCreate() {
    enableLoadMore = false;
    isShowNoMoreFooter = false;
  }

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.PROGRESS_TASK_LIST,
        isRefresh: false, method: FBResultFul.POST, params: {"stationTime": stationTimeBegin});
    return super.requestData();
  }

  @override
  List<DistributionEntity> transformList(List<DistributionEntity> dataList, bool isRefresh) {
    if (isRefresh || isFirstLoading) {
      Event.eventBus.fire(DispatcherGoodsCount(1, dataList.length));
    }
    return dataList;
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.PROGRESS_TASK_LIST,
        isRefresh: true, method: FBResultFul.POST, params: {"stationTime": stationTimeBegin});
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.PROGRESS_TASK_LIST,
        isRefresh: false, method: FBResultFul.POST, params: {"stationTime": stationTimeBegin});
  }

  void reset() {
    stationTimeBegin = '';
    stationTimeEnd.value = '';
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.PROGRESS_TASK_LIST,
        isRefresh: false, method: FBResultFul.POST, params: {"stationTime": stationTimeBegin});
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.PROGRESS_TASK_LIST,
        isRefresh: false, method: FBResultFul.POST, params: {"stationTime": stationTimeBegin});
  }

  void onItemClick(DistributionEntity itemData) {}

  void onExpand(DistributionEntity itemData) {}

  void onTapChild(DistributionChildEntity itemData) {
    requestUniversal<OrderDetailEntity>(
      Apis.ORDER_DETAIL,
      method: FBResultFul.GET,
      queryParameters: {"orderId": itemData.orderId},
      resultSuccessCall: (data)
      {
          Get.toNamed(Routers.progressDetailPage,arguments: { "OrderDetailEntity": data});
      },
    );
  }

  void withdraw(DistributionEntity itemData, int position) {
    requestUniversal<bool>(Apis.WITHDRAW,
        tipSuccess: MultiLanguage.withdrawalSucceeded.tr,
        isShowToastTip: true,
        method: FBResultFul.GET,
        queryParameters: {"id": itemData.id}, resultSuccessCall: (success) async {
      listData.remove(itemData);
      baseState!.setState(() {});
    });
  }
}
